#!/bin/sh

header_html()
{
extra_tpl_js=`echo "$extra_tpl_js" && cat <<'EOF'
$(function(){
  $('#post_login').on('submit', function(e){
    e.preventDefault();
    var data = "app=login&"+$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'post_login');
  });
});
EOF
`
home_data=`cat /tmp/home.json`
echo "$home_data" | grep -q '"apps":'
if
[ $? -ne 0 ]
then
main.sbin home_index
home_data=`cat /tmp/home.json`
fi

extra_tpl_css=`echo "$extra_tpl_css" && cat <<'EOF'   # 添加导航样式
.pure-menu li:hover { background: #7f8c8d; }

.pure-menu li:hover > ul {
    display: block;
    top: 0;
    left: 100%;
    visibility: visible;
}

.pure-menu li .pure-menu-children { border: 1px solid #b7b7b7; }

.pure-menu-horizontal > ul > li:hover > ul {
    top: 100%;
    left: 0;
}
EOF
`
extra_tpl_css=`echo "$extra_tpl_css" && cat <<'EOF'   # 网络状态栏样式
.progressdown
{
position:relative;
bottom :6px
}
.button-success {
            background: rgb(28, 184, 65);
        }
EOF
`

cat <<EOF

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="A layout example that shows off a responsive product landing page.">

    <title>登录 &ndash; ShellGui &ndash; Router Version</title>

<link rel="stylesheet" href="/apps/home/common/css/pure-min.css">
<link rel="stylesheet" href="/apps/home/common/css/common.css">

<!--[if lte IE 8]>
  
    <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/grids-responsive-old-ie-min.css">
  
<![endif]-->
<!--[if gt IE 8]><!-->
  
    <link rel="stylesheet" href="/apps/home/common/css/grids-responsive-min.css">
  
<!--<![endif]-->
  
    <!--[if lte IE 8]>
        <link rel="stylesheet" href="css/layouts/marketing-old-ie.css">
    <![endif]-->
    <!--[if gt IE 8]><!-->
        <link rel="stylesheet" href="/apps/home/common/css/marketing.css">
    <!--<![endif]-->

	<script src="/apps/home/common/js/jquery.min.js" type="text/javascript"></script>
	<script src="/apps/home/common/js/common.js" type="text/javascript"></script>

<script>
$extra_tpl_js
</script>
<style>
$extra_tpl_css
</style>
</head>
EOF

cat <<EOF			# 通知样式
<body>
<div class="notification-bar ajax-notification-bar"><a href="javascript:;" class="close">&times;</a></div>
EOF

cat <<EOF			# 导航条

<div class="header">
    <div class="home-menu pure-menu pure-menu-open pure-menu-horizontal pure-menu-fixed">

        <a class="pure-menu-heading" href="">登录</a>

        <ul>

                    <li class="pure-menu-can-have-children">
                        <a class="pure-menu-label" href="#">$lang</a>
                        <ul class="pure-menu-children">
EOF
. /usr/share/libubox/jshn.sh
json_cleanup
json_init
json_load "`cat /tmp/home.json`"
json_select i18n
json_select app
json_get_keys langs $FORM_app
langs=`echo "$langs" | sed 's/_/-/g'`
for lang_choise in $langs
do
[ "${lang_choise}" != "$lang" ] && cat <<EOF
<li><a a href="index.cgi?app=home&action=change&lang=${lang_choise}">${lang_choise}</a></li>
EOF
done
cat <<EOF
                        </ul>
                    </li>

        </ul>
    </div>
</div>
EOF
}
footer_html()
{
cat <<EOF
    <div class="footer l-box is-center" style="position:absolute;bottom:0;width:100%;">
	<a style="text-decoration:none;color:white;" href="https://www.shellgui.com/">关于ShellGui</a>|
	<a style="text-decoration:none;color:white;" href="https://www.shellgui.com/">联系我们</a>|
	<a style="text-decoration:none;color:white;" href="https://www.turbopi.com/">服务协议</a>|  涡轮派科技有限公司版权所有
    </div>
`[ "$FORM_app" = "home" ] && echo '</div>'`


</body>
</html>
EOF
}

main()
{
header_html
cat <<'EOF'

    <div class="content">
		<br/>

        <h2 class="content-head is-center">请登录,进入后台管理.</h2>

        <div class="pure-g">
            <div class="l-box-lrg pure-u-1 pure-u-md-2-5">
                <form class="pure-form pure-form-stacked" id="post_login">
                    <fieldset>
                        <label for="username">用户名</label>
                        <input id="username" name="username" type="text" placeholder="Username">

                        <label for="password">密码</label>
                        <input id="password" name="password" type="password" placeholder="Your Password">

						<input type="hidden" name="app" value="login">
						<input type="hidden" name="action" value="post_login">
                        <button type="submit" class="pure-button">登录</button>
                    </fieldset>
                </form>
            </div>

            <div class="l-box-lrg pure-u-1 pure-u-md-3-5">
            </div>
        </div>

    </div>

EOF
footer_html
}
post_login()
{
# [ -n "$FORM_username" ] && [ -n "$FORM_password" ] && 
if
cpasswd "$FORM_username" "$FORM_password" >/dev/null 2>&1
then
# cat <<EOF
# HTTP/1.0 302 Found
SESSION_ID=`main.sbin create_admin_session 2>/dev/null`
# {"status": 0,"msg": "登陆成功,正在跳转...", "jump_url": "index.cgi?app=home", "seconds": "2000"}
# EOF
export cookie_str="session=$SESSION_ID"
export jump_url="index.cgi?app=home"
export seconds="2000"
echo "登陆成功,正在跳转..." | main.sbin output_json 0

# main.sbin header_jump "index.cgi?app=home"
else
echo "密码错误还有9次机会" | main.sbin output_json 1 || exit 1
# main.sbin header_jump "index.cgi?app=login"
fi
}
post_logout()
{
main.sbin del_admin_session
main.sbin header_jump "index.cgi?app=login"
}

lang=`main.sbin get_client_lang`
eval `cat $DOCUMENT_ROOT/apps/$FORM_app/i18n/$lang/i18n.conf`
if
[ $is_main_page = 1 ]
then
main
elif [ -n "$FORM_action" ]
then
$FORM_action
fi
